<template>
    <div class="list">
        <PageHeader
            :title="$route.meta.title"
            :needLeft="true"
            :needRight="false"
        />
        <div class="list-container" @scroll="pageScroll" ref="homeWrap">
            <van-list
                v-model="loading"
                :finished="finished"
                finished-text="没有更多了"
                @load="onLoad"
            >
                <ul class="item-ul">
                    <li
                        class="item-li"
                        v-for="(item, i) in list"
                        :key="i"
                    >
                        <div class="img-box">
                            <img :src="item.spuInfo.images" alt="">
                        </div>
                        <div class="content-box">
                            <div class="title-box">
                                <h3>{{ item.spuInfo.spuTitle }}</h3>
                            </div>
                            <div class="sub-title-box">
                                <p>{{ item.spuInfo.spuSubTitle }}</p>
                            </div>
                            <div class="price-box">
                                <strong>{{ item.spuInfo.discountPrice | toFixed2 }}</strong>
                                <span>{{ item.spuInfo.price | toFixed2 }}</span>
                            </div>
                        </div>
                        <div class="tag-box" v-if="item.spuInfo.tagText !== ''">{{ item.spuInfo.tagText }}</div>
                        <router-link
                            tag="div"
                            :to="{name:'item', query:{id: item.spuInfo.skuId}}"
                            class="to-item"
                        />
                    </li>
                </ul>
            </van-list>
        </div>
        <div
            class="home-return-top"
            v-if="showReturnTop"
            @click="returnTop"
        >
            <ReturnTop />
        </div>
    </div>
</template>

<script>
import PageHeader from '@/components/PageHeader'
import ReturnTop from '@/components/ReturnTop'
export default {
    name: 'list',
    data () {
        return {
            list: [],
            page: 0,
            loading: false,
            finished: false,
            showReturnTop: false
        }
    },
    methods: {
        onLoad () {
            this.page += 1
            this.$http.getListData(this.$route.query.classifyid, this.page)
                .then(resp => {
                    // console.log(resp)
                    this.list = this.list.concat(resp.list)
                    this.loading = false
                    if (this.page >= resp.pageCount) {
                        this.finished = true
                    }
                })
        },
        pageScroll () {
            // console.log(this.$refs.homeWrap.scrollTop)
            if (this.$refs.homeWrap.scrollTop >= 700) {
                this.showReturnTop = true
            } else {
                this.showReturnTop = false
            }
        },
        returnTop () {
            // this.$refs.homeWrap.scrollTop = 0
            const start = this.$refs.homeWrap.scrollTop
            const timer = setInterval(() => {
                // console.log(this)
                this.$refs.homeWrap.scrollTop -= start * 0.2
                if (this.$refs.homeWrap.scrollTop <= 0) {
                    window.clearInterval(timer)
                }
            }, 25)
        }
    },
    // created () {
    //     // console.log(this.$route)
    //     this.$http.getListData(this.$route.query.classifyid, this.page)
    //         .then(resp => {
    //             // console.log(resp)
    //             this.list = resp.list
    //             // console.log(this.list)
    //         })
    // },
    components: {
        PageHeader,
        ReturnTop
    }
}
</script>

<style lang="scss">
.list {
    height: 100%;
    display: flex;
    flex-direction: column;
    .list-container {
        flex: 1;
        overflow-x: hidden;
        .item-ul {
            .item-li {
                position: relative;
                height: 156px;
                border-bottom: 1px solid #e9e9e9;
                background-color: #fff;
                box-sizing: border-box;
                padding: 15px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                .img-box {
                    width: 140px;
                    height: 140px;
                    img {
                        width: 100%;
                    }
                }
                .content-box {
                    flex: 1;
                    margin-left: 25px;
                    overflow: hidden;
                    .title-box {
                        overflow: hidden;
                        h3 {
                            font-size: 14px;
                            color: #262626;
                            font-weight: bold;
                            white-space: nowrap;
                            overflow: hidden;
                            text-overflow: ellipsis;
                        }
                    }
                    .sub-title-box {
                        margin-top: 10px;
                        p {
                            font-size: 10px;
                            color: #6c6c6c;
                        }
                    }
                    .price-box {
                        margin-top: 35px;
                        font-size: 12px;
                        font-weight: bold;
                        padding-bottom: 10px;
                        strong {
                            font-size: 14px;
                            color: #c73734;
                        }
                        span {
                            color: #999;
                            text-decoration: line-through;
                            margin-left: 10px;
                        }
                    }
                }
                .tag-box {
                    font-size: 10px;
                    padding: 5px;
                    background-color: #fee1df;
                    color: #be292c;
                    border: 1px solid #eb8682;
                    border-radius: 10px;
                    position: absolute;
                    top: 13px;
                    left: 13px;
                }
                .to-item {
                    position: absolute;
                    width: 100%;
                    height: 100%;
                    top: 0;
                    left: 0;
                }
            }
        }
    }
}
</style>
